package com.lzjy.tw.composedemo.ui.screen.home

import androidx.compose.animation.Crossfade
import androidx.compose.foundation.layout.height
import androidx.compose.material.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import com.google.accompanist.insets.navigationBarsHeight
import com.google.accompanist.insets.navigationBarsPadding
import com.lzjy.tw.composedemo.ui.nav.HomeTab
import androidx.compose.runtime.*
import androidx.hilt.navigation.compose.hiltViewModel
import com.lzjy.tw.composedemo.ui.screen.home.board.Board
import com.lzjy.tw.composedemo.ui.screen.home.note.Note
import timber.log.Timber


@Composable
fun HomeScreen(viewModel: HomeViewModel) {

    val select by remember {
        viewModel.selectedTab
    }

    val tabs = HomeTab.values()
    val selectedTab = HomeTab.getTabFromResource(select)


    Scaffold(
        topBar = {
            TopAppBar(
                elevation = 6.dp,
                modifier = Modifier.height(58.dp),
                title = { Text("App") },
            )
        },
        bottomBar = {
            BottomNavigation(
                modifier = Modifier
                    .navigationBarsHeight(56.dp)
            ) {
                tabs.forEach { tab ->
                    BottomNavigationItem(
                        icon = {
                            Icon(
                                imageVector = tab.icon,
                                contentDescription = null
                            )
                        },
                        label = { Text(text = stringResource(tab.title)) },
                        selected = tab == selectedTab,
                        onClick = { viewModel.selectTab(tab.title) },
                        selectedContentColor = LocalContentColor.current,
                        unselectedContentColor = Color.Gray,
                        modifier = Modifier.navigationBarsPadding()
                    )
                }
            }
        }

    ) {
        Crossfade(selectedTab) { destination ->
            when (destination) {
                // 点击笔记本进入纸张展示界面
                HomeTab.Note -> Note(
                    hiltViewModel(),
                    onSelect = {
                        Timber.e("Note 点击 $it")
                    }
                )
                HomeTab.Board -> Board()
            }
        }
    }
}